package com.kiragame.couplescreen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

public class MenuFragment extends SherlockFragment {
	// Layout
	private ListView lvContact;
	private Button btAdd;
	private TextView tvGetid;
	private TextView tvExit;
	private TextView tvRateapp;
	private TextView tvHideApp;

	private OnSelectMenuListener listener;

	private ArrayAdapter<String> adapter;
	public HashMap<String, String> contacts;
	private List<String> contactNames = new ArrayList<String>();

	public static MenuFragment newInstance() {
		MenuFragment fragment = new MenuFragment();
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.frament_sliding_menu, null);
		findViewIDs(v);
		init();
		setEventView();
		return v;
	}

	private void init() {
		getContact();
		adapter = new ArrayAdapter<String>(getActivity(), R.layout.simplerow,
				contactNames);
		lvContact.setAdapter(adapter);
	}

	private void getContact() {
		contactNames.clear();
		contacts = DataManager.getInstance(getActivity()).getAllContact();
		for (String contactName : contacts.keySet()) {
			contactNames.add(contactName);
		}
	}

	public void refreshContacts() {
		getContact();
		adapter.notifyDataSetChanged();
	}

	private void setEventView() {
		lvContact.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String contact = (String) parent.getAdapter().getItem(position);
				if (listener != null) {
					listener.onSelectContact(contact);
				}
			}
		});

		lvContact.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				String contact = (String) parent.getAdapter().getItem(position);
				if (listener != null) {
					listener.onEditContact(contact);
				}
				return true;
			}
		});

		btAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onAddContact();
				}
			}
		});
		
		tvRateapp.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onRateApp();
				}
			}
		});

		// tvGetid.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// if (listener != null) {
		// listener.onGetID();
		// }
		// }
		// });

		tvExit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onExit();
				}
			}
		});
		
		tvHideApp.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onHideApp();
				}
			}
		});
	}

	private void findViewIDs(View v) {
		lvContact = (ListView) v.findViewById(R.id.lv_contact);
		btAdd = (Button) v.findViewById(R.id.bt_add);
		tvGetid = (TextView) v.findViewById(R.id.tv_getid);
		tvExit = (TextView) v.findViewById(R.id.tv_exit);
		tvRateapp = (TextView) v.findViewById(R.id.tv_rateapp);
		tvHideApp = (TextView) v.findViewById(R.id.tv_hideapp);
	}

	public void setOnSelectMenuListener(OnSelectMenuListener listener) {
		this.listener = listener;
	}

	public interface OnSelectMenuListener {
		public void onSelectContact(String contact);

		public void onEditContact(String contact);

		public void onAddContact();

		public void onGetID();
		
		public void onRateApp();
		
		public void onHideApp();
		
		public void onExit();
	}

	public void showUserID() {
		int id = DataManager.getInstance(getActivity()).getUserID();
		String idText = "--";
		if (id != 0) {
			idText = Integer.toString(id);
		}
		tvGetid.setText("Your id: " + idText);
	}
}
